using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace lapinou_game2
{
    public class MainMenu : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch pictureMenu;
        Texture2D myMenuPicture;
        MouseState current_mouse;

        // mouse current relative position
        private int mouseX, mouseY;
        // mouse current pressed or released buttons status
        private bool leftClick, rightClick;

        public MainMenu()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 800; // hauteur de la fenetre
            graphics.PreferredBackBufferWidth = 600;  // largeur de la fenetre
        }

        protected override void Initialize()
        {
            // show mouse cursor on the menu
            this.IsMouseVisible = true;

            base.Initialize();
        }

        // method updating the scene
        protected override void Update(GameTime gameTime)
        {
            // call UpdateMouse to refresh mouse coords variables
            UpdateMouse();

            // if player clicks for new demo game
            if (getMouseLeftClick() && mouseIsOverRectangle(12, 16, 26, 13))
            {
                Game1 game = new Game1(this, this.Content, this.graphics);
                Components.Add(game);
            }
            base.Update(gameTime);
        }

        #region MousePositionning

        // method to know new mouse positions
        private void UpdateMouse()
        {
            // get mouse positions into an object
            current_mouse = Mouse.GetState();

            bool leftClick = false;
            bool rightClick = false;

            // stocks this coords into 2 differents variables

            setMousePosition(current_mouse.X, current_mouse.Y);
            setMouseButtons(current_mouse.LeftButton == ButtonState.Pressed, current_mouse.RightButton == ButtonState.Pressed);
        }

        public void setMouseButtons(bool leftClick, bool rightClick)
        {
            this.leftClick = (bool)leftClick;
            this.rightClick = (bool)rightClick;
        }

        public bool getMouseLeftClick()
        {
            return this.leftClick;
        }

        public bool getMouseRightClick()
        {
            return this.rightClick;
        }

        // method to set current mouse coords
        public void setMousePosition(int mouseX, int mouseY)
        {
            this.mouseX = (int)mouseX;
            this.mouseY = (int)mouseY;
        }

        // method to get current mouseX position
        public int getMouseX()
        {
            return this.mouseX;
        }

        // method to get current mouseY position
        public int getMouseY()
        {
            return this.mouseY;
        }

        // method which return true when mouse is over the rectangle(x1,y1,x2,y2)
        public bool mouseIsOverRectangle(int x1, int y1, int x2, int y2)
        {
            int mX = getMouseX();
            int mY = getMouseY();

            return ((mX >= x1 && mX <= x2) && (mY >= y1 && mY <= y2));
        }

        #endregion


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            // load background menu picture
            myMenuPicture = Content.Load<Texture2D>(@"Image/menu_avec");
        }


        protected override void UnloadContent()
        {
        }


        protected override void Draw(GameTime gameTime)
        {   
            base.Draw(gameTime);

            pictureMenu.Begin();
            pictureMenu.Draw(myMenuPicture, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
            pictureMenu.End();
        }
    }
}